@extends('base.base')
@section('base')
    <!-- 内容区域 -->
    <div class="main-panel">
        <div class="content-wrapper">
            <div class="page-header">
                <h3 class="page-title">
                     <span class="page-title-icon bg-gradient-primary text-white mr-2">
                        <i class="mdi mdi-settings"></i>
                    </span>
                    渠道来源统计
                </h3>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="/admin/channel/list?app_id={{$appId}}">渠道管理</a></li>
                        <li class="breadcrumb-item active" aria-current="page">渠道来源统计</li>
                    </ol>
                </nav>
            </div>
            <div class="row">
                <div class="col-lg-12 grid-margin stretch-card">
                    <div class="card">
                        <div class="card-body">
                            <h4 class="card-title">渠道来源统计列表</h4>
                            <div class="col-lg-9" style="float: left;padding: 0;">
                            </div>
                            <div class="col-lg-3" style="float: right">
                            </div>
                            <table class="table table-bordered">
                                <thead>
                                <tr>
                                    <th>APP名</th>
                                    <th>渠道名</th>
                                    <th>渠道链接</th>
                                    <th>类型</th>
                                    <th>来源</th>
                                    <th>次数</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($list as $v)
                                    <tr>
                                        <td>{{ $v['app_name'] }}</td>
                                        <td>
                                            {{ $v['channel_name'] }}
                                        </td>
                                        <td>
                                            {{ $v['channel_url'] }}
                                        </td>
                                        <td>
                                            @if($v["type"] == 1)
                                                <label class="badge badge-primary">Web</label>
                                            @elseif($v["type"] == 2)
                                                <label class="badge badge-dark">App</label>
                                            @else
                                            @endif
                                        </td>
                                        <td>{{ $v['url'] }}</td>
                                        <td>{{ $v['total'] }}</td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                            <div class="box-footer clearfix">
                                总共 <b>{{ $list->appends(["channel_id" => $channelId])->total()  }}</b> 条,分为<b>{{ $list->lastPage() }}</b>页
                                {!! $list->links() !!}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="//cdn.staticfile.org/clipboard.js/2.0.4/clipboard.min.js"></script>
    <script>
        $(function(){
            var clipboard = new ClipboardJS('.btn');

            clipboard.on('success', function(e) {
                layer.msg("链接复制成功!");//弹出提示
                e.clearSelection();
            });

            clipboard.on('error', function(e) {
                layer.msg("链接复制失败，请重试!");//弹出提示
            });

            cutStr(50);

            $('.mui-switch').click(function(){
                var type = $(this).data('type');
                var channelId = $(this).data('channel-id');
                var state = 0;
                if ($(this).prop('checked')) {
                    state = 1;
                }

                $.ajax({
                    url: "/admin/channel/updateFlag",
                    data: {type: type, channelId: channelId, state:state},
                    type: "POST",
                    dataType: "json",
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader("X-CSRF-TOKEN", $('meta[name="csrf-token"]').attr('content'));
                    },
                    success: function(data) {
                        if (data.errcode !=0) {
                            alert('参数错误')
                        }
                        // data = jQuery.parseJSON(data);  //dataType指明了返回数据为json类型，故不需要再反序列化
                    },
                    error: function () {
                        alert('更新失败')
                    }
                });
            })
        });
        function add(app_id){
            var page = layer.open({
                type: 2,
                title: '添加渠道',
                shadeClose: true,
                shade: 0.8,
                area: ['70%', '90%'],
                content: '/admin/channel/add?app_id='+app_id
            });
        }
        function update(id){
            var page = layer.open({
                type: 2,
                title: '修改渠道',
                shadeClose: true,
                shade: 0.8,
                area: ['70%', '90%'],
                content: '/admin/channel/update/'+id
            });
        }
        function del(id){
            myConfirm("删除操作不可逆,是否继续?",function(){
                myRequest("/admin/channel/del/"+id,"post",{},function(res){
                    layer.msg(res.msg)
                    setTimeout(function(){
                        window.location.reload();
                    },1500)
                });
            });
        }
    </script>
@endsection
